﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using MongoDb.EntityFramework;
using SManagement.Models;

namespace SManagement.Areas.References.Controllers
{
    public class ReferenceController<T> : Controller
        where T : TypeDesignation, new()
    {
        // GET: References/Reference
        public ActionResult Index()
        {
            return View();
        }

        // GET: References/Reference/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: References/Reference/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: References/Reference/Create
        [HttpPost]
        public ActionResult Create([Bind(Exclude = "_id")]T model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    MongoDbContext.Current.Add(model);
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: References/Reference/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: References/Reference/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, [Bind(Exclude = "_id")]T model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    MongoDbContext.Current.Update(model, m => m._id == model._id);
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: References/Reference/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: References/Reference/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, T model)
        {
            try
            {
                MongoDbContext.Current.Delete<T>(m => m._id == model._id);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}